package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;

// 接口文档 https://shimo.im/docs/K3WhpQ33RcqvkdyD/read
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/test")
    public User findById() {
        return userService.findById();
    }

    @PostMapping("/register")
    public ApiRestResponse register(
            @RequestParam("userName") String userName,
            @RequestParam("password") String password
    ) throws ImoocMallException, NoSuchAlgorithmException {

        if (StringUtils.isEmpty(userName)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }

        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }

        if (password.length() < 8) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }

        userService.register(userName, password);
        return ApiRestResponse.success();
    }

    @PostMapping("/login")
    public ApiRestResponse<User> login(
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            HttpSession session
    ) throws ImoocMallException {
        User user = userService.login(userName, password);
        user.setPassword(null);

        session.setAttribute(Constant.USER_SESSION, user);
        return ApiRestResponse.success(user);
    }

    @PostMapping("/user/update")
    public ApiRestResponse updateUserInfo(
            HttpSession session,
            @RequestParam("signature") String signature
    ) throws ImoocMallException {
        User currentUser = (User) session.getAttribute(Constant.USER_SESSION);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInformation(user);
        return ApiRestResponse.success();
    }

    @PostMapping("/user/logout")
    public ApiRestResponse logoutUser(HttpSession session) {
        session.removeAttribute(Constant.USER_SESSION);
        return ApiRestResponse.success();
    }

    @PostMapping("/adminLogin")
    public ApiRestResponse<User> adminLogin(
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            HttpSession session
    ) throws ImoocMallException {
        User user = userService.login(userName, password);
        // 校验是否是管理员
        if (userService.checkAdminUser(user)) {
            user.setPassword(null);

            session.setAttribute(Constant.USER_SESSION, user);
            return ApiRestResponse.success(user);
        }

        return ApiRestResponse.error(ImoocMallExceptionEnum.IS_ADMIN);
    }
}
